package com.enterprise.viewer.client.presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.event.shared.binder.EventBinder;


public class GadgetPresenter implements Presenter{

	interface MyEventBinder extends EventBinder<GadgetPresenter> {}
	static MyEventBinder eventBinder = GWT.create(MyEventBinder.class);

	public interface Display {
		void setURL(String url);
		HorizontalPanel getHeaderBar();
		HorizontalPanel getButtonBar();
		Label getLabel();
		Frame getFrame();
		Widget asWidget();
	}

	private final SimpleEventBus eventBus;
	private final Display display;


	public GadgetPresenter(SimpleEventBus eventBus, Display view){
		this.eventBus = eventBus;
		this.display = view;
		eventBinder.bindEventHandlers(this, eventBus);
		bind();
	}

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
	}

	private void bind(){

		History.addValueChangeHandler(new ValueChangeHandler<String>() {

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				//String token = event.getValue();
			}
		});
	}

	private class LoadProviderClickHandler implements ClickHandler {
		private String url;		

		public LoadProviderClickHandler(String url) {
			this.url = url;
		}

		@Override
		public void onClick(ClickEvent event) {
			
			/*//http://examples.javacodegeeks.com/enterprise-java/gwt/dynamic-flextable-example/
			
			FlexTable table = display.getContentPane();
			table.clear();	
			table.insertRow(0);
			table.addCell(0);
			table.addCell(0);
			table.insertRow(0);
			table.addCell(0);
			table.addCell(0);
			
			//http://stackoverflow.com/questions/9434836/access-frames-document-using-gwt
			//http://www.gwtproject.org/javadoc/latest/com/google/gwt/dom/client/IFrameElement.html
			
			//IFrameElement iframe = IFrameElement.as(frame.getElement());
			//iframe.getContentDocument();
			
			String s = "<iframe src='http://" + url + "' height='100%' width='100%' seamless='true' name='myframe' scrolling='no' frameborder='0'></iframe>";
		
			table.addCell(0);*/
		}
	}
}
